import 'package:dmz/base/controller/base_controller.dart';
import 'package:dmz/business/home/controller/home_main_controller.dart';
import 'package:dmz/business/home/model/moto_model.dart';
import 'package:dmz/business/home/widgets/%20wheel_scroll_widget.dart';
import 'package:dmz/business/home/widgets/run_message_widgets.dart';
import 'package:dmz/business/mine/model/lose_model.dart';
import 'package:dmz/request/apis.dart';
import 'package:dmz/request/request_client.dart';
import 'package:dmz/routers/routers.dart';
import 'package:dmz/user/user_defurl.dart';
import 'package:dmz/utils/customize_loding.dart';
import 'package:dmz/utils/keyboard_util.dart';
import 'package:dmz/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class LostUpdataController extends BaseController {
  @override
  void loadNet() {}

  @override
  void onHidden() {}

  @override
  void onInit() {
    motoList = Get.find<HomePageController>().motoList;
    contents[0] = types.first;
    motoSelect = Get.find<HomePageController>().pageIndex;
    contents[1] = motoList![motoSelect].name ?? '';
    contents[2] = lostDate = SPUtils.getTimeStr();
    contents.last = EMIBUserManger().user?.phone ?? '';
    getLoseListData(0);
    super.onInit();
  }

  List types = ['车辆失窃'.tr, '电池失窃'.tr];
  int lostType = 0;
  String lostAddressStr = '';
  String lostDate = '';
  String userPhoneStr = '';

  List<String> contents = ['', '', '', '', ''];
  List<MotoModel>? motoList;
  int motoSelect = -1;
  int motoSave = -1;
  TextEditingController textController = TextEditingController();
  List<LoseModel>? loseList;
  LoseModel? selectModel;

  inforSelects(int tag) {
    switch (tag) {
      case 0:
        upDataType(tag);
        break;
      case 1:
        selectMoto(tag);
        break;
      case 2:
        selectDate(tag);
        break;
      case 3:
        lostAddress(tag);
        break;
      case 4:
        break;
      default:
    }
  }

  ///失窃上报类型
  upDataType(int tag) {
    EMILoding.showBottomWindow(
        SelectWheelWidget(
            title: '失窃上报类型'.tr,
            initIndex: 0,
            count: 2,
            contnetSize: 38.sp,
            datas: types,
            callBack: (index, data) {
              Get.back();
              lostType = index;
              contents[tag] = '${types[tag]}';
              update(['lostUpdata']);
            }),
        isDismissible: true,
        showBack: true);
  }

  ///车辆失窃
  selectMoto(int tag) {
    motoSelect = motoSave;
    Get.toNamed(RouteGet.motoSelect);
  }

  didSelect(int index) {
    motoSelect = index;
    update(['lostSelect']);
  }

  saveSelect() {
    motoSave = motoSelect;
    contents[1] = motoList?[motoSave].name ?? '';
    update(['lostUpdata']);
    Get.back();
  }

  ///失窃时间
  selectDate(int tag) {
    EMILoding.showBottomWindow(
        RunMessageWidgets.seleDateWheelWidget(
            lostDate.isEmpty ? SPUtils.getTimeStr() : lostDate, (value) {
          ///时间选择完成
          contents[tag] = lostDate = value;
          Get.back();
          update(['lostUpdata']);
        }),
        isDismissible: true,
        showBack: true);
  }

  ///失窃地点
  lostAddress(int tag) {
    textController.text = contents[tag];
    Get.toNamed(RouteGet.addressInput)!.then((value) {
      textController.text = '';
    });
  }

  saveAddress() {
    KeyboardUtils.hideKeyboardGex();
    lostAddressStr = contents[3] = textController.text;
    update(['lostUpdata']);
    Get.back();
  }

  bottomClick() {
    selectModel != null ? didunlock() : didUpdata();
  }

  didUpdata() {
    if (lostAddressStr.isEmpty) {
      return EMILoding.hitLoding('地址不可为空'.tr);
    }
    EMILoding.hitBottomWidget('确定提交失窃上报吗？'.tr, '失窃上报后将自动锁定车辆'.tr, (agree) {
      if (agree) {
        RequestClient().post<Map>(APIS.loseUpdata, data: {
          'ebike_id': motoList?[motoSelect].id ?? 0,
          'lose_time': SPUtils.getTimeInt(lostDate),
          'addr': lostAddressStr,
          'lose_type': lostType + 1
        }).then((value) {
          if (value != null) {}
        });
      }
    });
  }

  didunlock() {
    EMILoding.showBottomHitWindow('确定解除失窃上报吗？'.tr, '解除失窃后将自动解锁车辆'.tr, (agree) {
      Get.back();
      if (agree) {
        RequestClient().post<Map>(APIS.unlock, data: {
          'ebike_id': motoList?[motoSelect].id ?? 0,
        }).then((value) {
          if (value != null) {}
        });
      }
    }, isDismissible: true);
  }

  getLoseListData(int index, {bool reload = false}) async {
    var value = await RequestClient()
        .post<LosePageData>(APIS.loseList, data: {'index': index, 'size': 20});
    loseList = value?.list ?? [];
    if (value?.list?.first.ebike_id == motoList?[motoSelect].id &&
        value?.list?.first.lock_status == 1) {
      selectModel = value?.list?.first;
      lostType = (selectModel!.lose_type! - 1);
      contents[0] = types[lostType];
      motoSelect = 0;
      update(['lostUpdata']);
    }
    if (reload) update(['loseList']);
  }

  turnLoseHistory() {
    Get.toNamed(RouteGet.loseHistory);
  }
}
